package com.example.webapp.controller;

import com.example.webapp.service.HttpDebugService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/debug")
@Api(tags = "Debug Controller")
public class DebugController {

    @Autowired
    private HttpDebugService httpDebugService;

    @PostMapping("/apitest")
    @ApiOperation("测试接口")
    public ResponseEntity<String> testApi(@RequestBody Map<String, Object> request) {
        try {
            String url = (String) request.get("url");
            String method = (String) request.get("method");
            
            @SuppressWarnings("unchecked")
            Map<String, String> headers = (Map<String, String>) request.get("headers");
            
            @SuppressWarnings("unchecked")
            Map<String, String> params = (Map<String, String>) request.get("params");
            
            Object body = request.get("body");
            
            return httpDebugService.sendRequest(url, method, headers, params, 
                body != null ? body.toString() : null);
        } catch (Exception e) {
            log.error("Error testing API: ", e);
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
} 